﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Kalficom.Forms
{
    public partial class frmGeographicZone : Form
    {
        public frmGeographicZone()
        {
            InitializeComponent();
            conf.Icon(this);
        }

        Libraies.db db = new Libraies.db();
        Query.qry_zone query = new Query.qry_zone();

        private void btnMainMenu_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if ((new Forms.frmGeographicZone_CreateModify()).ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Read user record from database
                db.FillDataSet(query.get, tblZone, false);
            }
        }

        private void frmGeographicZone_Load(object sender, EventArgs e)
        {
            // Read zone record from database
            db.FillDataSet(query.get, tblZone);

            this.WindowState = FormWindowState.Maximized;
        }

        private void dgUser_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            Forms.frmGeographicZone_CreateModify frm = new Forms.frmGeographicZone_CreateModify();
            frm.Tag = dgZone.Rows[e.RowIndex].Cells["zone_id"].Value;

            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Read user record from database
                db.FillDataSet(query.get, tblZone, false);
            }
        }

        private void dgUser_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == dgZone.Columns["colDelete"].Index)
            {
                Libraies.msg msg = new Libraies.msg();
                if (int.Parse(db.ExecuteScalar(string.Format(query.get_contractzone, dgZone.CurrentRow.Cells["zone_id"].Value))) > 0)
                {
                    MessageBox.Show(msg.zone_inUsed, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (MessageBox.Show(msg.deleteMsg, msg.captionMsg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    // Delete zone from database.
                    db.ExecuteNonQuery(string.Format(query.deleted, dgZone.CurrentRow.Cells["zone_id"].Value));

                    // Delete zone record from list.
                    tblZone.Select(string.Format("zone_id={0}", dgZone.CurrentRow.Cells["zone_id"].Value))[0].Delete();
                }
            }
        }
    }
}
